//统一封装接口函数
import request from "@/utils/request"

//登录接口
export const loginAPI = ({ mobile, code}) => request({
    url: '/v1_0/authorizations',
    method: 'POST',
    data: {
        mobile,
        code
    }
})

//用户 - 刷新token
export const getNewTokenAPI = () => request({
    url: '/v1_0/authorizations',
    method: 'put',
    headers: {
        Authorization: 'Bearer ' + localStorage.getItem('refresh_token')
        // Authorization: 'Bearer ' + getStorage('refresh_token')
    }
})


//获取所有频道列表
export const getAllChannelsAPI = () => request({
    url: '/v1_0/channels',
})

//获取用户频道
export const getUserChannelsAPI = () => request({
    url: '/v1_0/user/channels',
})

//获取文章列表
export const getAllArticleListAPI = ({ channel_id, timestamp}) => request({
    url: '/v1_0/articles',
    params: {
        channel_id,
        timestamp
    }
})

//文章反馈不感兴趣
export const dislikeArticleAPI = ({ artId }) => request({
    url: 'v1_0/article/dislikes',
    method: 'post',
    data: {
        target: artId
    }
})

//反馈垃圾内容
export const reportArticleAPI = () => request({
    url: '/v1_0/article/reports',
    method: 'post',
    data: {
        target: artId,
        type: type,
        remark: 'xxx'
    }
})

//更新覆盖频道
export const updateChannelsAPI = ({ channels }) => request({
    url: '/v1_0/user/channels',
    method: 'put',
    data: {
        channels
    }
})

//删除频道
export const removeTheChannelAPI = ({ channelId }) => request({
    url: `/v1_0/user/channels/${channelId}`,
    method: 'delete'
})

//搜索
export const suggestListAPI = ({ keywords }) => request({
    url: '/v1_0/suggestion',
    params: {
       q: keywords 
    }
})

//搜索结果
export const searchResultAPI = ({ page = 1, per_page = 10, q }) => request({
    url: '/v1_0/search',
    params: {
        page,
        per_page,
        q
    }
})

//获取文章详情
export const detailAPI = ({ artId }) => request({
    url: `/v1_0/articles/${artId}`
})

//关注
export const userFollowedAPI = ({ userId }) => request({
    url: '/v1_0/user/followings',
    method: 'post',
    data: {
        target: userId
    }
})

//取关
export const userUnFollowedAPI = ({ userId }) => request({
    url: `/v1_0/user/followings/${userId}`,
    method: 'delete'
})

//点赞
export const likeArticleAPI = ({ artId }) => request({
    url: '/v1_0/article/likings',
    method: 'post',
    data: {
        target: artId
    }
})

//取消点赞
export const unLikeArticleAPI = ({ artId }) => request({
    url: `/v1_0/article/likings/${artId}`,
    method: 'delete'
})

// 获取评论或评论回复
export const commentsListAPI = ({ id, offset = null, limit }) => request({
    url: '/v1_0/comments',
    params: {
        type: 'a',
        source: id,
        offset,
        limit
    }
})

//文章评论点赞
export const commentLikingAPI = ({ comId }) => request({
    url: '/v1_0/comment/likings',
    method: 'post',
    data: {
        target: comId
    }
})
//文章评论取消点赞
export const commentDisLikingAPI = ({ comId }) => request({
    url: `/v1_0/comment/likings/${comId}`,
    method: 'delete'
})

//文章评论发布
export const commentSendAPI = ({ id, content, art_id = null }) => {
    // axios 中，data 请求体传参，如果值为null是不会忽略这个参数的
    //也会把参数和值携带给后台（只有parans参数时遇null才会忽略
    //data请求体对象需要自己处理
    const obj = {
        target: id,
        content
    }
    if(art_id !== null) {
        obj.art_d = art_id
    }
    return request({
        url: '/v1_0/comments',
        method: 'post',
        data: obj
    })
}

//获取用户个人资料
export const userProfileAPI = () => request({
    url: '/v1_0/user/profile',
})

//用户基本信息
export const getUserInfoAPI = () => request({
    url: '/v1_0/user',
})

//用户更新头像
export const updateUserPhotoAPI = (fd) => request({
    url: '/v1_0/user/photo',
    method: 'patch',
    data: fd
})

//更新用户名称
export const updateUserProfileAPI = (dataObj) => {
    //判断有什么值，再带什么参数给后台
    const obj = {
        name: '',
        gender: 0,
        birthday: '',
        intro: ''
    }
    for( const prop in obj ) {
        if( dataObj[prop] === undefined) {
            delete obj[prop]
        } else {
            obj[prop] =dataObj[prop]
        }
    }
    return request({
        url: '/v1_0/user/profile',
        method: 'patch',
        data: obj
    })
} 













